_ = require 'underscore'

module.exports = (app, User, School, Vocab, Grammar, Activity) ->
  app.post '/data/get', (req, res) ->
    get_data req, res, User, School
  app.post '/vocab/save', (req, res) ->
    save_vocab req, res, User
  app.post '/school/languages/save', (req, res) ->
    save_language req, res, School
  app.post '/school/languages/remove', (req, res) ->
    remove_language req, res, School
  app.post '/school/settings/student/save', (req, res) ->
    save_student_settings req, res, School
  app.post '/school/student/save', (req, res) ->
    save_student req, res, User, School

get_data = (req, res, User, School) ->
  #TODO: Is this being used?
  user = User.find { un: req.body.u }, (err, docs) ->
    unless docs.length is 1 and docs[0].tk is req.body.t
      res.json { error: true }
      return
    user = docs[0]
    lang = req.body.l
    cls = req.body.c
    json = {}
    Vocab.find { la: lang, cl: cls }, ['co','tr','ca'], (err, docs) ->
      json.v = docs
    Grammar.find { la: lang, cl: cls }, ['sd','us','ex','eg'], (err, docs) ->
      json.g = docs
    Activities.find { la: lang, cl: cls }, ['sd','us','ex','eg'], (err, docs) ->
      json.a = docs
    user_class = _.find user.uc, (el) -> el.la is lang and el.cl is cls
    if user_class
      for p in user_class.pr
        el = _.find json.v, (el) -> el._id == p.id
        el.pr = p.pr
      json.pt = user_class.pt
    # Combine everything with class stuff
    teachers = []
    if user.sc
      User.find { tc: true, sc: user.sc, cl: { $contains: "#{lang}_#{cls}" } }, ['uc'], (err, docs) ->
        teachers = docs
    for teacher in teachers
      for vocab in teacher.cv
        json.v.push vocab
      for grammar in teacher.cg
        json.g.push cg
      for activity in teacher.ca
        json.g.push ca
    # And with your own custom stuff
    for vocab in user.cv
      json.v.push vocab
    for grammar in user.cg
      json.g.push grammar
    for activity in user.ca
      json.a.push activity

    res.json json

save_vocab = (req, res, User) ->
  user = User.find { un: req.body.u }, (err, docs) ->
    unless docs.length is 1 and docs[0].tk is req.body.t
      res.json { error: true }
      return
    user = docs[0]
    #for vocab in req.body.v
      #TODO: Replace old progress with new
    #TODO: Save user

recalculate_points = (progress) ->
  points = 0
  for pr in progress
    points += pr.pr - 50 if pr.pr > 50

# School

ensure_valid_school = (req, res, School, callback) ->
  School.find { si: req.body.i, tk: req.body.tk }, (err, docs) ->
    unless docs.length is 1
      res.json { error: true }
      return
    callback docs[0]

save_language = (req, res, School) ->
  ensure_valid_school req, res, School, (school) ->
    unless (language = LANGUAGE_ID[req.body.la]) and req.body.cl
      res.json { error: 2 }
      return
    school.sl.remove { la: language }
    school.sl.push { la: language, cl: req.body.cl.split(',') }
    if school.errors
      res.json { error: 3 }
    else
      school.save (err) ->
        res.json { error: if err then yes else no }

remove_language = (req, res, School) ->
  ensure_valid_school req, res, School, (school) ->
    unless (language = LANGUAGE_ID[req.body.la])
      res.json { error: 2 }
      return
    #TODO: WHY THE HELL DOESN'T THIS WORK?
    school.sl.remove { la: language }
    if school.errors
      res.json { error: 3 }
    else
      school.save (err) ->
        res.json { error: if err then yes else no }

save_student_settings = (req, res, School) ->
  ensure_valid_school req, res, School, (school) ->
    school.sp = req.body.p
    school.se = req.body.e
    if school.errors
      res.json { error: yes }
    else
      school.save (err) ->
        res.json { error: if err then yes else no }

save_student = (req, res, User, School) ->
  ensure_valid_school req, res, School, (school) ->
    for required_property in ['un', 'rn', 'em', 'pw', 'tc', 'cl']
      unless req.body[required_property]
        res.json { error: true }
        return
    user = new User()
    user.un = school.si + '.' + req.body.un
    user.rn = req.body.rn
    user.em = req.body.em
    user.pw = req.body.pw
    user.tc = req.body.tc
    for cls in req.body.cl
      [la, cl] = cls.split '_'
      user.uc.push {
        la: la
        cl: cl
      }
      user.cl.push cls
    user.save (err) ->
      console.log "Tried saving ..."
      console.log err
      res.json { error: if err then yes else no }


